package com.example.moments.ssmemo.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.example.moments.ssmemo.ContentData;

import java.util.List;
//
//import en.edu.zucc.pb.listviewpengyouquanv3.R;
//import en.edu.zucc.pb.listviewpengyouquanv3.customerwidget.AutoWrapLineLayout;
//import en.edu.zucc.pb.listviewpengyouquanv3.databean.CommentData;
//import en.edu.zucc.pb.listviewpengyouquanv3.databean.ContentData;
//import en.edu.zucc.pb.listviewpengyouquanv3.databean.LikeData;
//import en.edu.zucc.pb.listviewpengyouquanv3.databean.LinkData;
import com.example.moments.R;
import com.example.moments.ssmemo.entity.CommentData;
import com.example.moments.ssmemo.entity.LikeData;
import com.example.moments.ssmemo.entity.LinkData;
import com.example.moments.widget.AutoWrapLineLayout;

public class PengyouquanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int HEADER_VIEW = 0;
    private static final int NORMAL_VIEW = 1;

    private Context context;
    private List<ContentData> dataList;

    public PengyouquanAdapter(Context context, List<ContentData> data) {
        this.context = context;
        this.dataList = data;
    }

//    这个 样子什么都无所谓吗  只要 是一个view
    public class HeaderViewHolder extends RecyclerView.ViewHolder {

        public HeaderViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Do whatever you want on clicking the item
                }
            });
        }
    }

    // Now define the ViewHolder for Normal list item
    public class NormalViewHolder extends RecyclerView.ViewHolder {
        public NormalViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Do whatever you want on clicking the normal items
                }
            });
        }

        public TextView username;
        public TextView content;
        public FrameLayout container;
        public LinearLayout link;
        public LinearLayout likeOrComment;
        public LinearLayout comment;

        public TextView getUsername() {
            return username;
        }

        public void setUsername(TextView username) {
            this.username = username;
        }

        public TextView getContent() {
            return content;
        }

        public void setContent(TextView content) {
            this.content = content;
        }

        public FrameLayout getContainer() {
            return container;
        }

        public void setContainer(FrameLayout container) {
            this.container = container;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == HEADER_VIEW) {
//            View headerView = LayoutInflater.from(context).inflate(R.layout.header, null);
            View headerView = LayoutInflater.from(context).
                    inflate(R.layout.explore_pengyouquan_head_item_starp, null);
            HeaderViewHolder Headerholder = new HeaderViewHolder(headerView);
            return Headerholder;
        }

        View normView = LayoutInflater.from(context).inflate(R.layout.data_item_layout, null);
//        View normView = LayoutInflater.from(context).inflate(R.layout.dataitem_layout, null);
        NormalViewHolder holder = new NormalViewHolder(normView);
        holder.username = (TextView) normView.findViewById(R.id.username);
        holder.content = (TextView) normView.findViewById(R.id.content);
        holder.container = (FrameLayout) normView.findViewById(R.id.container);
        holder.link = (LinearLayout) normView.findViewById(R.id.linkcontent);
        holder.likeOrComment = (LinearLayout) normView.findViewById(R.id.likeOrComment);
        holder.comment = (LinearLayout) normView.findViewById(R.id.comment);
        return holder;
    }

//    void putManyPic(   ContentData data,  NormalViewHolder holder){
//
////                        多个图片
//        GridView images = new GridView(context);
//        images.setAdapter(new GridImageAdapter(context, data.getImages()));
//        images.setPadding(0, 0, 0, 0);
//        images.setNumColumns(3);
//        int row = 1;
//        if (data.getImages().size() == 4) {
//            images.setNumColumns(2);
//            row = 2;
//        } else if (data.getImages().size() > 6) {
//            row = 3;
//        } else if (data.getImages().size() > 3) {
//            row = 2;
//        }
//        //指定gridview高度
//        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dip2px(context, 88 * row));
//        images.setLayoutParams(params);
//        holder.container.removeAllViews();
//        holder.container.addView(images);
//        holder.container.setVisibility(View.VISIBLE);
//    }
//

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        try {
            if (viewHolder instanceof NormalViewHolder) {
                NormalViewHolder holder = (NormalViewHolder) viewHolder;
                ContentData data = dataList.get(position - 1);
                //填充用户名称
                holder.username.setText(data.getUsername());

                //填充用户发布信息
                if (data.getContent() != null) {
                    holder.content.setText(data.getContent());
                    holder.content.setVisibility(View.VISIBLE);
                } else {
                    holder.content.setVisibility(View.GONE);
                }

                //填充用户发布的连接信息
                if (data.getLinkData() != null) {
                    final LinkData linkData = data.getLinkData();
                    ImageView linkimg = (ImageView) holder.link.findViewById(R.id.linkimg);
                    linkimg.setImageResource(linkData.getLinkimg());
                    TextView linkTitle = (TextView) holder.link.findViewById(R.id.linktitle);
                    linkTitle.setText(linkData.getLinktitle());
                    linkTitle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("Link Click", "将要跳转到:" + linkData.getUrl());
                            Toast.makeText(context, "将要跳转到:" + linkData.getUrl(), Toast.LENGTH_SHORT).show();
                        }
                    });
                    holder.link.setVisibility(View.VISIBLE);
                } else {
                    holder.link.setVisibility(View.GONE);
                }

                //填充用户发布图片信息
                if (data.getImages() != null) {
                    if (data.getImages().size() > 1) {
//                        putManyPic(    data,   holder);
//
////                        多个图片
                        GridView images = new GridView(context);
                        images.setAdapter(new GridImageAdapter(context, data.getImages()));
                        images.setPadding(0, 0, 0, 0);
                        images.setNumColumns(3);
                        int row = 1;
//                        根据图片的数量 调整他的 行列的数量
                        if (data.getImages().size() == 4) {
                            images.setNumColumns(2);
                            row = 2;
                        } else if (data.getImages().size() > 6) {
                            row = 3;
                        } else if (data.getImages().size() > 3) {
                            row = 2;
                        }
                        //指定gridview高度
                        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dip2px(context, 88 * row));
                        images.setLayoutParams(params);
                        holder.container.removeAllViews();
                        holder.container.addView(images);
                        holder.container.setVisibility(View.VISIBLE);
                    } else if (data.getImages().size() == 1) {
                        ImageView image = new ImageView(context);
                        image.setLeft(0);
                        image.setImageResource(data.getImages().get(0));
                        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        image.setScaleType(ImageView.ScaleType.FIT_START);

                        Drawable drawable = context.getResources().getDrawable(data.getImages().get(0), null);
                        int dwidth = drawable.getIntrinsicWidth();
                        int dheight = drawable.getIntrinsicHeight();
                        if (dheight > dwidth) {
                            int theight = dip2px(context, 180);
                            int twidth = theight * dheight / dwidth;
                            params.height = theight;
                            params.width = twidth;
                        } else {
                            int twidth = dip2px(context, 180);
                            int theight = twidth * dheight / dwidth;
                            params.height = theight;
                            params.width = twidth;
                        }
                        image.setLayoutParams(params);
                        holder.container.removeAllViews();
                        holder.container.addView(image);
                        holder.container.setVisibility(View.VISIBLE);
                    }
                } else {
                    holder.container.setVisibility(View.GONE);
                }

                boolean isShowCommentOrLike = false;
                //填充评论点赞数据
                AutoWrapLineLayout likelinLay = (AutoWrapLineLayout) holder.likeOrComment.findViewById(R.id.like);
                likelinLay.removeAllViews();

                LinearLayout comment = (LinearLayout) holder.likeOrComment.findViewById(R.id.comment);
                comment.removeAllViews();
//                这个有数据 就会显示 like之类的
                if (data.getLikeData() != null) {
                    likelinLay.setFillMode(AutoWrapLineLayout.MODE_WRAP_CONTENT);
                    for (LikeData item : data.getLikeData()) {
                        View likeItem = LayoutInflater.from(context).inflate(R.layout.like, null);
                        TextView userName = (TextView) likeItem.findViewById(R.id.username);
                        userName.setText(item.getUsername());
                        likelinLay.addView(likeItem);
                    }
                    isShowCommentOrLike = true;
                }

                if (data.getCommentDatas() != null) {
                    for (CommentData item : data.getCommentDatas()) {
                        View comentItem = LayoutInflater.from(context).inflate(R.layout.comment, null);
                        if (item.getReplyUsername() != null) {
                            comentItem = LayoutInflater.from(context).inflate(R.layout.replycomment, null);
                            //      填充了 被回复的人
                            TextView replyusername = (TextView) comentItem.findViewById(R.id.replyusername);
//                            if(item)
                            if(replyusername!=null){
                                replyusername.setText(item.getReplyUsername());
                            }

                        }
                        TextView content = (TextView) comentItem.findViewById(R.id.content);
                        TextView username = (TextView) comentItem.findViewById(R.id.username);
                        if(content!=null){
                            content.setText(item.getContent());
                        }
                      if(username!=null){
                          username.setText(item.getUsername());

                      }
                        comment.addView(comentItem);
                    }
                    isShowCommentOrLike = true;
                }

                if (isShowCommentOrLike) {
                    holder.likeOrComment.setVisibility(View.VISIBLE);
                } else {
                    holder.likeOrComment.setVisibility(View.GONE);
                }

            } else if (viewHolder instanceof HeaderViewHolder) {
                HeaderViewHolder headerViewHolder = (HeaderViewHolder) viewHolder;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        if (dataList == null) {
            return 0;
        }

        if (dataList.size() == 0) {
            //header count include
            return 1;
        }

        //header count include
        return dataList.size() + 1;
    }

//    头部，根据位置 是0 的就当作是 头部
//    其他是一般的
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER_VIEW;
        }

        return NORMAL_VIEW;
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}